package projet.item;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import projet.Tools;

import projet.CustomException;
import projet.dataaccess.DataAccess;
import projet.dataaccess.PathErrorException;

/**
 * The Video class represents a Video object. Information about the Video is
 * stored and can be retrieved. We assume that we only deal with movie Videos at
 * this stage.
 * 
 * @author BRUN Sylvain
 * @version Janvier 27, 2010
 */
public class Video extends Item {

	private Integer time;
	private String format;

	/**
	 * Create an empty Video.
	 * 
	 * @param id The future item's ID.
	 * @throws CustomException
	 */
	public Video(int id) throws CustomException {
		super();
		this.id = id;
		this.AskEdit();
	}

	/**
	 * Initialize the Video.
	 * 
	 * @param id The future video.
	 * @param theTitle The title video.
	 * @param comment The comment of the video.
	 * @param format The format of the video.
	 * @param path The path of the video.
	 */
	public Video(int id, String theTitle, String comment, int time,
			String format, String path) {
		super(id, theTitle, comment, path);
		this.time = time;
		this.format = format;
		this.path = path;
	}

	/**
	 * @param dataAccess
	 * @throws PathErrorException
	 */
	public Video(DataAccess dataAccess) throws PathErrorException {
		super(dataAccess);
		this.time = dataAccess.getElementInt("time");
		this.format = dataAccess.getElementString("format");
		this.path = dataAccess.getElementString("path");

	}

	/**
	 * Set the time, format and path
	 * 
	 * @throws CustomException
	 */
	@Override
	public void AskEdit() throws CustomException {
		super.AskEdit();

		// Time
		System.out.print("- Time: ");
		this.time = Integer.parseInt(Tools.readLineEdit(this.time));

		// Format
		System.out.print("- Format: ");
		this.format = Tools.readLineEdit(this.format);

	}

	/**
	 * @return The time
	 */
	public int getTime() {
		return this.time;
	}

	/**
	 * @return The artist for this video.
	 */
	public String getFormat() {
		return this.format;
	}

	/**
	 * read the media
	 */
	@Override
	public void play() {
		if (Desktop.isDesktopSupported()) {
			if (Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
				try {
					Desktop.getDesktop().open(new File(path));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/*
	 * (non-Javadoc)
	 * @see projet.item.Item#toMediaAccess()
	 */
	public DataAccess toMediaAccess() throws PathErrorException {
		DataAccess dataAccess = super.toMediaAccess();
		dataAccess.addElement("format", this.format);
		dataAccess.addElement("path", this.path);
		dataAccess.addElement("time", this.time.toString());

		return dataAccess;
	}

	/**
	 * Méthode affichage pour un show
	 */
	public void affichage() {
		super.affichage();
		Tools.print(2, "Format      : " + this.format);
		Tools.print(2, "Duration    : " + this.time);
	}

	/**
	 * @return The String of the informations of the video
	 */
	@Override
	public String toString() {
		return super.toString() + "Time:\"" + this.time + "\" Format:\""
				+ this.format;
	}

}
